<template>
    <div class="searchDlg">
        <el-input v-model="keyword" class="w-50 m-8 search-input" size="small" @keydown="searchUser"
            placeholder="输入用户昵称或id，按回车键搜索" :suffix-icon="Search" />
        <div class="userlist">
            <div class="useritem" v-for="item in userlist">
                <div class="userInfo">
                    <img class="avatar" width="50" height="50" :src="Util.getImgUrl(item.avatar)">
                    <div class="userDetails">
                        <span>昵称: {{ item.nickname }}</span>
                        <span>ID: {{ item.username }}</span>
                    </div>
                </div>
                <el-icon @click="() => addFriend(item.username)" class="plus" v-show="!friendlist.includes(item.username)">
                    <Plus />
                </el-icon>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { Util, getUserInfo } from '@/Util';
import { userInfo } from '@/Util/Constant';
import eventBus from '@/Util/EventBus';
import axios from 'axios';
import { Search, Plus } from '@element-plus/icons-vue'

let myName = getUserInfo().username
const keyword = ref('')
let userlist = ref<userInfo[]>([])
let friendlist = ref<string[]>([])
onMounted(() => {
    getFriendList()
})
const getFriendList = async () => {
    const { data } = await axios.get('/chat/friends', {
        params: {
            username: myName
        }
    })
    friendlist.value = data.data
    eventBus.emit('setFriendList', data.data)
}

const searchUser = async (e: KeyboardEvent) => {
    if (e.key === 'Enter') {
        let kwd = keyword.value.trim()
        if (kwd) {
            const { data } = await axios.get('/user/search', {
                params: {
                    keyword: kwd,
                    username: myName
                }
            })
            userlist.value = data.data
        }
    }
}

const addFriend = async (friendName: string) => {
    const { data } = await axios.post('/chat/friends', {
        username: myName,
        friendName
    })
    friendlist.value = data.data
    eventBus.emit('updateFriendList')
    eventBus.emit('setFriendList', data.data)
}
</script>


<style scoped lang="less">
.searchDlg {
    padding: 15px;
    height: 100%;
    display: flex;
    flex-direction: column;

    .search-input {
        font-size: 14px;
        width: 100%;
        height: 30px;
        margin-bottom: 15px;
    }

    .userlist {
        display: flex;
        flex-wrap: wrap;
        overflow: auto;
        justify-content: space-between;

        .useritem {
            color: #fff;
            width: 45%;
            padding: 5px 10px;
            background-color: rgba(0, 0, 0, .3);
            border-radius: 5px;
            margin: 0 10px 10px;
            display: flex;
            align-items: center;
            justify-content: space-between;

            .userInfo {

                display: flex;
                align-items: center;

                .avatar {
                    border-radius: 50%;
                    margin-right: 5px;
                }

                .userDetails {
                    font-size: 12px;
                    display: flex;
                    flex-direction: column;
                    justify-content: space-around;

                    span {
                        margin: 2px 0;
                    }
                }
            }

            .plus {
                cursor: pointer;
                width: 25px;
                height: 25px;
                border-radius: 50%;
                background-color: #fff;
                color: #393636;
                text-align: center;
                vertical-align: bottom;
                font-weight: bold;
            }
        }
    }
}
</style>
